package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.im.huanxin.EMChatLoginUtils;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.util.phone.PhoneUtil;


/**
 *
 */

public class DialogLoanConsult extends Dialog {
    public DialogLoanConsult(Context context) {
        super(context);
    }

    public DialogLoanConsult(Context context, int themeResId) {
        super(context);
    }

    protected DialogLoanConsult(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        /**
         * Helper class for creating a custom dialog
         */

        private Activity activity;
        private String title;


        public Builder(Activity activity) {
            this.activity = activity;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public DialogLoanConsult.Builder setTitle(int title) {
            this.title = (String) activity.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public DialogLoanConsult.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public DialogLoanConsult create() {
            final DialogLoanConsult dialog = new DialogLoanConsult(activity, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            Window window = dialog.getWindow();
            //取消标题
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //取消状态栏
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setBackgroundDrawable(new ColorDrawable());
            window.setContentView(R.layout.dialog_loan_consult);
            ImageView ivClose = (ImageView) window.findViewById(R.id.dlc_iv_close);
            TextView ivOfflineContact = (TextView) window.findViewById(R.id.dlc_iv_offline_contact);
            TextView ivOnlineContact = (TextView) window.findViewById(R.id.dlc_iv_online_contact);
            Drawable topOnLineDrawable = ContextCompat.getDrawable(activity, R.drawable.icon_online_contact);
            topOnLineDrawable.setBounds(0, 0, FRDisplayMetrics.dp2px(activity, 125.0f), FRDisplayMetrics.dp2px(activity, 125.0f));
            ivOnlineContact.setCompoundDrawables(null, topOnLineDrawable, null, null);
            Drawable topOffLineDrawable = ContextCompat.getDrawable(activity, R.drawable.icon_offline_contact);
            topOffLineDrawable.setBounds(0, 0, FRDisplayMetrics.dp2px(activity, 125.0f), FRDisplayMetrics.dp2px(activity, 125.0f));
            ivOfflineContact.setCompoundDrawables(null, topOffLineDrawable, null, null);
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            ivOfflineContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    PhoneUtil.callPhone(activity, FRSharedPreferences.getStringNotClear(SPConstant.CUSTOMER_SERVICE, InfoConstant.CUSTOMER_SERVICE_NUM));
                }
            });

            ivOnlineContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    EMChatLoginUtils.getInstance().chatWithService(activity, R.color.colorPrimary);
                }
            });
            return dialog;
        }
    }
}
